﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LabLibary;
using LabManager.Controller.Views;
using LabManager.Controller;
using LabManager.Models;
using System.IO.Ports;

namespace LabManager
{
    public partial class frmCauHinh : DevExpress.XtraEditors.XtraForm, CauHinhView
    {

        CauHinhController _cauhinhController = new CauHinhController();
        Dictionary<string, int> dcPort = new Dictionary<string, int>();

        public string ComportNumber { get; set; }
        public string ComportValue { get; set; }

        public string LabMachineName
        {
            get { return cbMayXN.Text; }
            set { cbMayXN.Text = value; }
        }

        public string PortName
        {
            get { return cbLstPort.Text; }
            set { cbLstPort.Text = value; }
        }

        public int _BauRate
        {
            get { try { return Convert.ToInt32(cbSpeeds.Text); } catch { return -1; } }
            set { cbSpeeds.Text = value.ToString(); }
        }

        public int _DataBits
        {
            get { try {return Convert.ToInt32(cbLstDataBits.Text); } catch{return -1;} }
            set { cbLstDataBits.Text = value.ToString(); }
        }

        public Parity _Parity {
            get { return (Parity)Enum.Parse(typeof(System.IO.Ports.Parity), cbLstParity.Text); }
            set { cbLstParity.Text = value.ToString(); } 
        }

        public StopBits _StopBits
        {
            get { return (StopBits) Enum.Parse(typeof(System.IO.Ports.StopBits), cbStopBits.Text); }
            set { cbStopBits.Text = value.ToString(); }
        }

        public Handshake _HandShaking
        {
            get { return (Handshake)Enum.Parse(typeof(System.IO.Ports.Handshake), cbHandShaking.Text); }
            set { cbHandShaking.Text = value.ToString(); }
        }

        #region Cac Ham
        void InitDictionnay()
        {
            dcPort.Add("Cổng 1", 0);
            dcPort.Add("Cổng 2", 1);
            dcPort.Add("Cổng 3", 2);
            dcPort.Add("Cổng 4", 3);
            dcPort.Add("Cổng 5", 4);
            dcPort.Add("Cổng 6", 5);
            dcPort.Add("Cổng 7", 6);
            dcPort.Add("Cổng 8", 7);
        }
        #endregion


        public frmCauHinh()
        {
            InitializeComponent();
        }

        private void frmCauHinh_Load(object sender, EventArgs e)
        {
            InitDictionnay();

            this.Text += ComportNumber;

            LabCommon.InitLabMachine(cbMayXN);

            LabCommon.InitListPortName(cbLstPort);
            LabCommon.InitSpeed(cbSpeeds);
            LabCommon.InitDataBits(cbLstDataBits);
            LabCommon.InitParity(cbLstParity);
            LabCommon.InitStopBits(cbStopBits);
            LabCommon.InitHandShaking(cbHandShaking);


            try
            {
                //Init Default
                ComportInfo _comInfo = _cauhinhController.LoadConfig()[dcPort[ComportNumber]];
                LabMachineName = _comInfo.LabMachineName;
                PortName = _comInfo.portName;
                _BauRate = _comInfo._Baurate;
                _DataBits = _comInfo._DataBits;
                _Parity = _comInfo._Parity;
                _StopBits = _comInfo._StopBits;
                _HandShaking = _comInfo._HandShaking;
            }
            catch { }

        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (LabMachineName == string.Empty)
            {
                MessageBox.Show("Vui lòng chọn Máy xét nghiệm cho " + ComportNumber);
                return;
            }

            if (PortName == string.Empty)
            {
                MessageBox.Show("Vui lòng chọn PortName cho " + ComportNumber);
                return;
            }

            if (_BauRate == -1)
            {
                MessageBox.Show("Vui lòng chọn Max Speed cho " + ComportNumber);
                return;
            }

            if (_DataBits == -1)
            {
                MessageBox.Show("Vui lòng chọn Max Speed cho " + ComportNumber);
                return;
            }

            _cauhinhController._comportInfo.LabMachineName = LabMachineName;
            _cauhinhController._comportInfo.portName = PortName;
            _cauhinhController._comportInfo._Baurate = _BauRate;
            _cauhinhController._comportInfo._DataBits = _DataBits;
            _cauhinhController._comportInfo._Parity = _Parity;
            _cauhinhController._comportInfo._StopBits = _StopBits;
            _cauhinhController._comportInfo._HandShaking = _HandShaking;

            _cauhinhController.Save(dcPort[ComportNumber]);
        }

        private void btbnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

    }
}